package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.VideoCardReply;
import reduce.fz.com.reduce.bean.WitnessCardReply;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.WitnessReplyEvent;
import reduce.fz.com.reduce.fragment.TeamDetailFragment;
import reduce.fz.com.reduce.fragment.VideoDetailFragment;
import reduce.fz.com.reduce.service.VideoService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class VideoCardDetailActivity extends BaseToolBarActivity {
    private RelativeLayout mRLReply;
    private FrameLayout mMainContainer;
    //回复内容编辑框
    private EditText mEtReplyContent;
    //回复button
    private TextView mTvReplyBtn;
    //
    private VideoDetailFragment fragment;
    private VideoCard videoCard;
    private Long cardid;
    private VideoService videoService = ServiceBuilder.builderVideoService(null);

    /**
     * 发送评论监听
     */
    public void loadVideoCard() {
        final LoadingDialog dialog = new LoadingDialog(this);
        dialog.show();
        videoService.getVideo(cardid, new AbResponseListener<VideoCard>(new TypeReference<Result<VideoCard>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<VideoCard> data) {
                videoCard = data.getData();
                attachFragment();
                dialog.dismiss();

            }
        });
    }


    public void attachFragment() {
        fragment = VideoDetailFragment.create(videoCard);
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container, fragment).commit();
    }

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(mEtReplyContent.getText().toString()))
                mTvReplyBtn.setTextColor(getResources().getColor(R.color.text_gray3));
            else
                mTvReplyBtn.setTextColor(getResources().getColor(R.color.text_blue));
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_card_detail);
        setToolbarTitle("详情");
        cardid=getIntent().getLongExtra("cardid",0l);
        initWidget();
        initReplyBtnListener();
        loadVideoCard();
    }

    @Override
    public void initWidget() {
        mRLReply = (RelativeLayout) findViewById(R.id.rl_reply);
        mMainContainer = (FrameLayout) findViewById(R.id.main_container);
        mEtReplyContent = (EditText) findViewById(R.id.et_reply_content);
        mTvReplyBtn = (TextView) findViewById(R.id.tv_reply_btn);
        initEditListener();
    }

    public void initEditListener() {
       /* mEtReplyContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取焦点
                InputMethodUtil.reuestFocusAndShowInput(mEtReplyContent);
            }
        });*/
    }

    /**
     * 初始化
     */
    public void initReplyBtnListener() {
        mEtReplyContent.addTextChangedListener(watcher);
        mTvReplyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VideoCardReply reply = new VideoCardReply();
                reply.description = mEtReplyContent.getText().toString();
                reply.cardid=videoCard.cardid;
                //清除焦点并隐藏输入法
               // InputMethodUtil.clearFocus(mEtReplyContent);
                final LoadingDialog loadingDialog = new LoadingDialog(VideoCardDetailActivity.this);
                loadingDialog.setMsg("提交中,请稍候!").show();
                videoService.videoReply(reply, new AbResponseListener<VideoCardReply>(new TypeReference<Result<VideoCardReply>>() {
                }) {
                    @Override
                    public void error(ServiceException e) {
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void responseInfo(Result<VideoCardReply> data) {
                        loadingDialog.dismiss();
                        mEtReplyContent.setText("");
                        VideoCardReply reply=data.getData();
                        reply.user= UserPreference.getUser();
                        fragment.addLast(data.getData());

                    }
                });
            }
        });
    }
    @Override
    public void initListener() {

    }


}
